{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c8951d24c307fc3e",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/data_connectors/GoogleMapsTextSearchReaderDemo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9c676c0",
   "metadata": {},
   "source": [
    "# Google Maps Text Search Reader\n",
    "This notebook demonstrates how to use the GoogleMapsTextSearchReader from the llama_index library to load and query data from the Google Maps Places API."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e9c676c1",
   "metadata": {},
   "source": [
    "If you're opening this Notebook on colab, you will need to install the llama-index library."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea0e003b",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install llama-index llama-index-readers-google"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88141371-de4c-4c02-9e8f-10d2491b5a33",
   "metadata": {},
   "source": [
    "### Importing Necessary Libraries\n",
    "We will import the necessary libraries including the GoogleMapsTextSearchReader from llama_index and other utility libraries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6b62adf",
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging\n",
    "import sys\n",
    "from llama_index.readers.google import GoogleMapsTextSearchReader\n",
    "from llama_index.core import VectorStoreIndex\n",
    "from IPython.display import Markdown, display\n",
    "import os\n",
    "\n",
    "logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n",
    "logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "018d3fd6",
   "metadata": {},
   "source": [
    "### Setting Up API Key\n",
    "Make sure you have your Google Maps API key ready. You can set it directly in the code or store it in an environment variable named `GOOGLE_MAPS_API_KEY`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8f0a9f2-c6a9-4840-a38a-0b2f8e433063",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set your API key here if not using environment variable\n",
    "os.environ[\"GOOGLE_MAPS_API_KEY\"] = api_key"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8f0a9f3-c6a9-4840-a38a-0b2f8e433063",
   "metadata": {},
   "source": [
    "### Loading Data from Google Maps\n",
    "Using the `GoogleMapsTextSearchReader`, we will load data for a search query. In this example, we search for quality Turkish food in Istanbul."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89ef1fac-aa36-4f5f-b5cf-bc4dfa0bd332",
   "metadata": {},
   "outputs": [],
   "source": [
    "loader = GoogleMapsTextSearchReader()\n",
    "documents = loader.load_data(\n",
    "    text=\"I want to eat quality Turkish food in Istanbul\",\n",
    "    number_of_results=160,\n",
    ")\n",
    "\n",
    "# Displaying the first document to understand its structure\n",
    "print(documents[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2c1573f-2e49-49e8-8daf-19e6f1777eaa",
   "metadata": {},
   "source": [
    "### Indexing the Loaded Data\n",
    "We will now create a VectorStoreIndex from the loaded documents. This index will allow us to perform efficient queries on the data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d4533c9-9020-4f50-837c-316ec2c454f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "index = VectorStoreIndex.from_documents(documents)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2c1573f-2e49-49e8-8daf-19e6f1777eab",
   "metadata": {},
   "source": [
    "### Querying the Index\n",
    "Finally, we will query the index to find the Turkish restaurant with the best reviews."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d4533c9-9020-4f50-837c-316ec2c454f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = index.query(\"Which Turkish restaurant has the best reviews?\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d4533c9-9020-4f50-837c-316ec2c454f4",
   "metadata": {},
   "source": [
    "### Summary\n",
    "In this notebook, we demonstrated how to use the GoogleMapsTextSearchReader to load data from Google Maps, index it using the VectorStoreIndex, and perform a query to find the best-reviewed Turkish restaurant in Istanbul."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
